/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controlelrl;

import br.ufrn.dca.controle.QuanserClient;
import br.ufrn.dca.controle.QuanserClientException;
import gui.TSinais;
import java.awt.Dimension;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.DefaultXYDataset;

/**
 *
 * @author Rafael Holanda
 */
public class Tanque implements Runnable{

    public static final int NUM_VALUES = 60;
    public static final int HEAVISIDE = 0;
    public static final int SIN = 1;
    public static final int RECT = 2;
    public static final int SAWTOOTH = 3;
    public static final int RANDOM_SIGNAL = 4;
    
        
    private int signalType;
    private double minAmp;
    private double maxAmp;
    private double period;
    private double randomPeriod = 0;
    private double randomValue;
    
    @Override
    public void run() {
        JFreeChart grafico;
        ChartPanel p;
        
        double[][] valuesEscrita = new double[2][NUM_VALUES];
        double[][] valuesLeitura = new double[2][NUM_VALUES];

//        signalType = 2;
//        maxAmp = 3;
//        minAmp = -3;
        period = 2;
        
        QuanserClient quanserClient;
        try {
            quanserClient = new QuanserClient("localhost", 20081);
        
            // X values
            for (int i=0; i<NUM_VALUES; i++)
            {
                valuesEscrita[0][i] = i / 10.0;
                valuesLeitura[0][i] = i / 10.0;
            }

            // Y values
    //        for (int i=0; i<NUM_VALUES; i++)
    //        {
    //            sineValues[1][i] = Math.sin(sineValues[0][i]);
    //            cosineValues[1][i] = Math.cos(cosineValues[0][i]);
    //        }

            DefaultXYDataset dataset = new DefaultXYDataset();

            try {
                for (int i=0; i<NUM_VALUES; i++) {

                    double value = calculateValue(valuesEscrita[0][i]);
                    if (value > 3)
                        value = 3;

                    if (value < 3)
                        value = -3;

                    double valorLido = quanserClient.read(0) * 6.25;
                    System.out.println(valorLido);
                    if (value < 0 && valorLido <= 2)
                        value = 0;

                    valuesEscrita[1][i] = value;

                    quanserClient.write(0,value);

                    valuesLeitura[1][i] = quanserClient.read(0) * 6.25;

                    dataset.addSeries("Escrita", valuesEscrita);
                    dataset.addSeries("Leitura", valuesLeitura);

                    grafico = ChartFactory.createXYLineChart(
                            "Sine / Cosine Curves", // The chart title
                            "X", // x axis label
                            "Y", // y axis label
                            dataset, // The dataset for the chart
                            PlotOrientation.VERTICAL,
                            true, // Is a legend required?
                            false, // Use tooltips
                            false // Configure chart to generate URLs?
                        );

                    p = new ChartPanel(grafico);
                    
                    p.setPreferredSize(new Dimension(400, 400));
                    
                    JPanel panel = new JPanel();
                    panel.add(p);
                    
                    TSinais tSinais = ControleLRL.getFrame().getTSinaisPanel();
                    tSinais.setGraficoPanel(panel);
                    ControleLRL.getFrame().setContentPane(tSinais);
                    ControleLRL.getFrame().revalidate();
                    ControleLRL.getFrame().repaint();
                    
                    Thread.sleep(100);

                }
            } catch (InterruptedException ex) {
                Logger.getLogger(Tanque.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (QuanserClientException ex) {
            ex.printStackTrace();
        }
        
    }
    public double calculateValue(double j) {
        switch (signalType) {
            case HEAVISIDE:
                return maxAmp;
            case SIN:
                return ((maxAmp - minAmp) / 2) * Math.sin((j) / (period / (2 * Math.PI))) + ((maxAmp + minAmp) / 2);
            case RECT:
                return j % period < period / 2 ? maxAmp : minAmp;
            case SAWTOOTH:
                //writeValue = j - Math.floor(j);
                return ((((maxAmp - minAmp) / period) * j) % (maxAmp - minAmp)) + minAmp;
            case RANDOM_SIGNAL:
                randomSignal();
                return randomValue;
            default:
                return 0;
        }
    }
    private void randomSignal() {
        if (randomPeriod <= 0) {
            randomPeriod = Math.random() * period;
            randomValue = Math.random() * 3;
        } else {
            randomPeriod--;
        }
    }

    public void setMaxAmp(double maxAmp) {
        this.maxAmp = maxAmp;
    }

    public void setMinAmp(double minAmp) {
        this.minAmp = minAmp;
    }

    public void setPeriod(double period) {
        this.period = period;
    }

    public void setRandomPeriod(double randomPeriod) {
        this.randomPeriod = randomPeriod;
    }

    public void setRandomValue(double randomValue) {
        this.randomValue = randomValue;
    }

    public void setSignalType(int signalType) {
        this.signalType = signalType;
    }
    
}
